<?php
include ("configs/config.php");
if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
	{
	header("Location: login-admin.php");
	exit();
	}

$tpl = new XTemplate("./template/tpl_admin.html");
$tpl->parse("main.body.menu");
$error=0;
$tpl->assign("edit_picture","Manage Archive Content");
if ($_GET["clicked"]==1)
{
	
		//$title=$_GET["title"];
		$description=$_GET["description"];
		$url=$_GET["url"];	
		$picture_title=$_GET["picture_title"];
		$id=$_GET["id"];
		$title=$_GET["title"];
		$old_title=$_GET["old_title"];
		
		if ($_GET["type"]=="Banner")
		{	
			
	
			if ($picture_title<>"" and $title <> "" and $url <> "")
			{
			
				$query=('update pictures set url="'.$url.'",picture_title="'.$picture_title.'" ,name="'.$title.'" where id='.$id.';');
				mysql_query($query);
				
			}
		}	
		
		if ($_GET["type"]=="Picture")
		{
			if ($title<>"")
			{
				$query=('update pictures set description="'.$description.'",picture_title="'.$picture_title.'",picture_url="'.$url.'", name="'.$title.'" where id='.$id.';');
				mysql_query($query);
				
				
			}
		}
		
		if ($_GET["type"]=="Link")
		{
			$query='update pictures set picture_title="'.$picture_title.'",url="'.$url.'" where id='.$id.';';
			mysql_query($query);
	
			
		}
		
		if (mysql_affected_rows()<1)
		{
			$error++;
			//$msg_error="There was a problem with the database! Please come back in 10 minutes";
		}
		else
		{
//			echo $PATH."pictures/$old_title.jpg </br>";
//			echo $PATH."pictures/$title.jpg  </br>";
			@ rename($PATH."pictures/$old_title.jpg",$PATH."pictures/$title.jpg");
			@ rename($PATH."pictures/thumb-$old_title.jpg",$PATH."pictures/thumb-$title.jpg");	
			
			header("Location: edit-archive.php");
			exit();
		}
	
	
}


if ($_GET["archive"]<>"")
{	
	$id=$_GET["archive"];
	 $id++;
	$query="update pictures set id=id+1 order by id desc";
	
	mysql_query($query);
	$query ="update pictures_group set pictures=pictures+1 order by pictures desc";
	
	mysql_query($query);
	$rez=mysql_query("select min(id) from pictures");
	$rez1=mysql_fetch_array($rez);
	
	$rez1[0]--;
	$query="update pictures set id=$rez1[0],first_page=0 where id=$id";
	
	mysql_query($query);
	$query="update pictures_group set pictures=$rez1[0] where pictures=$id";
	mysql_query($query);
	
	
	
}




if($_GET["first_page"]<>"")
{
	$id=$_GET["id"];

	if ($_GET["first_page"]=="yes")
	{
	
		$query=("update pictures set first_page=0 where id=$id");
	}
	else
	{
		$query=("update pictures set first_page=1 where id=$id and validation=1");
	}
	
	mysql_query($query)	;

	if (mysql_affected_rows()<1)
	{
		$error++;
		$msg_error="The picture is not validated or there was a problem with the database";
		
	}
	else
	{
		header("Location: edit-archive.php");
		exit();
	}

}

if (isset($_GET["validate"]))
{
	if ($_GET["validate"]=="invalidate")
		{$query=("update pictures set validation=".$_GET["validate"].", first_page=0 where id=".$_GET["id"].";");}
	else
		{$query=("update pictures set validation=".$_GET["validate"].", first_page=0 where id=".$_GET["id"].";");}

	mysql_query($query);
	if (mysql_affected_rows()<1)
	{
		$error++;
		$msg_error="There was a problem with the database! Please come back in 10 minutes";
		
	}
	else
	{
		header("Location: edit-archive.php");
		exit();
	}

	
}

if ($_GET["delete"]<>"" )
{
	$id=$_GET["delete"];
	$query=("delete from pictures where id=".$id.";");
	mysql_query($query);
	$query="delete from pictures_group where pictures=$id";
	mysql_query($query);

	if (mysql_affected_rows()==-1)
	{
		$error++;
		$msg_error="There was a problem with the database! Please come back in 10 minutes";
			
	}
	else
	{
		$title=$_GET["title"];
		@ unlink($PATH."pictures/$title.jpg");
		@ unlink($PATH."pictures/thumb-$title.jpg");
		
		header("Location: edit-archive.php");
		exit();
	}
}



if ($_GET["position"]<>"")
{
	if ($_GET["position"]=="up")
	{
		$id=$_GET["id"];
		move_record($id,'desc','pictures','id');
	}
	else
	{
		$id=$_GET["id"];
	 	move_record($id,"asc","pictures","id");
	}	 
	 
	header("Location: edit-archive.php");
	exit();	
}

if ($_SESSION["entries"]=="")
{
		$_SESSION["entries"]=30;
}
else	
{
	if ($_POST["entries"]<>"")
	{
		$_SESSION["entries"]=$_POST["entries"];
	}
	
}	




$entries=$_SESSION["entries"];
$tpl->assign("entries",$entries);
$query=("select * from pictures where first_page=0 order by id  desc limit 0,$entries;");
$result=mysql_query($query);
if (mysql_num_rows($result)>0)
{
	while($picture=mysql_fetch_array($result))
	{
		$tpl->assign("id",$picture["id"]);
		$date=explode(" ",$picture["add_date"]);
		$tpl->assign("date",$date[0]);
		
		if ($picture["validation"]==0)
		{
			$tpl->assign("validate","Validate");
			$tpl->assign("validate_value","1");
		}
		else
		{
			$tpl->assign("validate","Invalidate");
			$tpl->assign("validate_value","0");
		}
		
		if ($picture["first_page"]==0)
		{
			$tpl->assign("first_page","No");
			$tpl->assign("first_page_value","no");
		}
		else
		{
			$tpl->assign("first_page","Yes");
			$tpl->assign("first_page_value","yes");
		}
		
	
		if ($picture["name"]=="")
			{
				$tpl->assign("picture_style","link_style");
				$tpl->assign("type","Link");
				$tpl->assign("url",$picture["url"]);
				$tpl->assign("picture_title",$picture["picture_title"]);
				$tpl->assign("title","");
				$tpl->assign("description","");
				
			}
			
		else
		{
			if	($picture["url"]<>"")
			{
				$tpl->assign("type","Banner");
				$tpl->assign("picture_style","banner_style");
				$tpl->assign("url",$picture["url"]);
				$tpl->assign("title",stripslashes($picture["name"]) );
				$tpl->assign("picture_title",$picture["picture_title"]);
				$tpl->assign("description","");
				$tpl->assign("old_title",$picture["name"]);
			}
			
			else
			{
				$tpl->assign("type","Picture");
				$tpl->assign("picture_style","picture_style");
				$tpl->assign("description",stripslashes($picture["description"]));
				$tpl->assign("title",stripslashes($picture["name"]) );
				$tpl->assign("picture_title",$picture["picture_title"]);
				$tpl->assign("url",$picture['picture_url']);
				$tpl->assign("old_title",$picture["name"]);
			}	
		}		
					
	$tpl->parse("main.body.edit_picture.row");	
	}
}


$tpl->assign("msg_error",$msg_error);
$tpl->parse("main.body.edit_picture.error");
$tpl->parse("main.body.edit_picture");
$tpl->parse("main.body");
$tpl->parse("main");
$tpl->out("main");
?>